package simulator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeSet;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer;
import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class BoxAndWhisker extends ApplicationFrame {
	private static final long serialVersionUID = 1L;

	public BoxAndWhisker(final String title, HashMap<Net, HashMap<Double, List<Statistics>>> globalStats) {

        super(title);
        final BoxAndWhiskerCategoryDataset dataset = createDataset(title, globalStats);
        
        final CategoryAxis xAxis = new CategoryAxis("Alpha");
        final NumberAxis yAxis = new NumberAxis("% " + title);
        //yAxis.resizeRange(0, 100);
        yAxis.setRange(0, 100);
        yAxis.setAutoRange(false);
   //     yAxis.setAutoRangeIncludesZero(false);
        final BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
        renderer.setFillBox(false);
        renderer.setMaximumBarWidth(0.03);
        final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
        final JFreeChart chart = new JFreeChart(plot);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(450, 270));
        setContentPane(chartPanel);

    }

    private BoxAndWhiskerCategoryDataset createDataset(String title, HashMap<Net, HashMap<Double, List<Statistics>>> globalStats) {
        

        final DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset();
        
        for (Net n: globalStats.keySet()) {
        	HashMap<Double, List<Statistics>> netStats = globalStats.get(n);
            TreeSet<Double> alphas = new TreeSet<Double>(netStats.keySet());
            for (Double alpha: alphas) {
            	final List<Double> list = new ArrayList<Double>();
            	for(Statistics stats: netStats.get(alpha)){
            		
            		if(title.equals("Disponibilidad")){
            			list.add(stats.getDispTime()/stats.getTotalTime() *100.0);
            		
            		} else if(title.equals("Infectadas")){
            			list.add(stats.getInfected()/stats.getTotalTime() *100.0);
            		
            		} else if(title.equals("Rotas")){
            			list.add(stats.getBroken()/stats.getTotalTime() *100.0);
            		}  
            	}
            	dataset.add(list, n.toString(), alpha);
            }
            
        }


        
        return dataset;
    }
    
    public static void generateGraph(String title, HashMap<Net, HashMap<Double, List<Statistics>>> globalStats) {

    	final BoxAndWhisker demo = new BoxAndWhisker(title, globalStats);
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }
    

}